package com.mz.ozy.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.adapter.GoodsDetailSpecAdapter;
import com.mz.ozy.adapter.SpecTagAdapter;
import com.mz.ozy.bean.GoodsSpecBean;
import com.mz.ozy.bean.ShopSpecListBean;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.mz.ozy.widget.dialog.BasePopupWindow;
import com.mz.ozy.widget.flowlayout.FlowLayout;
import com.mz.ozy.widget.flowlayout.TagFlowLayout;

import org.jetbrains.annotations.NotNull;

import java.util.List;


/**
 * 规格参数
 */
public class SpecVipWindow extends BasePopupWindow implements View.OnClickListener {
    private ImageView goodsImage;
    private TextView tvGoodsPrice;
    private TextView tvGoodsNum;
    private TextView numberTv;
    private TextView tv_confirm;
    private TextView joinCarTv;
    private int number = 1;
    private OnClickListener mOnClickListener;
    private ShopSpecListBean mSpecListBeans;
    private SpecTagAdapter mSearchTagAdapter;
    private int ggId;
    private String name;
    private final int mOpType;
    private int mPosition;

    public SpecVipWindow(Context mContext, ShopSpecListBean specListBeans, int opType, int position, OnClickListener mOnClickListener) {
        super(mContext, R.layout.pop_vip_spec, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        this.mSpecListBeans = specListBeans;
        if (mSpecListBeans == null) {
            mSpecListBeans = new ShopSpecListBean();
        }
        this.mOpType = opType;
        this.mPosition = position;
        this.mOnClickListener = mOnClickListener;
        init();
    }

    private void init() {

        goodsImage = getView(R.id.shopImage);
        tvGoodsPrice = getView(R.id.tv_goods_price);
        tvGoodsNum = getView(R.id.tv_goods_num);
        numberTv = getView(R.id.numberTv);
        tv_confirm = getView(R.id.tv_confirm);
        joinCarTv = getView(R.id.joinCarTv);

        TextView lessTv = getView(R.id.lessTv);
        TextView addTv = getView(R.id.addTv);

        if (mOpType != 2) {
            joinCarTv.setVisibility(View.GONE);
            tv_confirm.setText("确定");
        }
        tvGoodsPrice.setText(mSpecListBeans.getPrice());

        tv_confirm.setOnClickListener(this);
        joinCarTv.setOnClickListener(this);
//        getView(R.id.buyNowTv).setOnClickListener(this);
        View view = getView(R.id.view);
        ImageView ivGoodSpecClose = getView(R.id.iv_goods_spec_close);
        view.setOnClickListener(this);
        ivGoodSpecClose.setOnClickListener(this);
        if (mSpecListBeans.isOperate()) {
            lessTv.setOnClickListener(this);
            addTv.setOnClickListener(this);
        }
        TagFlowLayout mFlowTagLayout = getView(R.id.flowTagLayout);
        final String goodsImg = mSpecListBeans.getGoodsImg();
        if (!TextUtils.isEmpty(goodsImg)) {
            GlideUtils.loadImg(mContext, goodsImage, goodsImg);
        }
        final List<ShopSpecListBean.ShopSpecBean> specBeanList = mSpecListBeans.getList();
        if (specBeanList != null) {
            if (specBeanList.size() > 0) {
                ggId = specBeanList.get(mPosition).getId();
                name = specBeanList.get(mPosition).getName();
                tvGoodsNum.setText(mContext.getString(R.string.in_stock_format, specBeanList.get(mPosition).getStock() + ""));
            }
            mSearchTagAdapter = new SpecTagAdapter(mContext, mFlowTagLayout, specBeanList, mPosition);
            mFlowTagLayout.setAdapter(mSearchTagAdapter);
            mFlowTagLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    mSearchTagAdapter.setSelectPosition(position);
                    final ShopSpecListBean.ShopSpecBean shopSpecBean = specBeanList.get(position);
                    mPosition = position;
                    ggId = shopSpecBean.getId();
                    name = shopSpecBean.getName();
                    tvGoodsPrice.setText(getPrice(shopSpecBean));
                    tvGoodsNum.setText(mContext.getString(R.string.in_stock_format, shopSpecBean.getStock() + ""));
                    return false;
                }
            });
        }
//        }

    }

    @NotNull
    private String getPrice(ShopSpecListBean.ShopSpecBean bean) {
        String price = "";
        if (!TextUtils.isEmpty(bean.getPrice()) && Double.parseDouble(bean.getPrice()) != 0.00) {
            price += "¥";
            price += bean.getPrice();
        }
        if (!TextUtils.isEmpty(bean.getMarket_price()) && Double.parseDouble(bean.getMarket_price()) != 0.00) {
            if (!TextUtils.isEmpty(price)) {
                price += "+";
                price += bean.getMarket_price();
                price += "积分";
            } else {
                price += bean.getMarket_price();
                price += "积分";
            }
        }
        if (TextUtils.isEmpty(price)) {
            price = "¥0.00";
        }
        return price;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_goods_spec_close:
            case R.id.view:
                mOnClickListener.onSubmit(ggId, name, 2, mPosition);
                dismiss();
                break;
            case R.id.tv_confirm:
                if (mOpType == 2 || mOpType == 1) {
                    mOnClickListener.onSubmit(ggId, name, 1, mPosition);
                } else {
                    mOnClickListener.onSubmit(ggId, name, 0, mPosition);
                }
                dismiss();
                break;
            case R.id.joinCarTv:
                mOnClickListener.onSubmit(ggId, name, 0, mPosition);
                dismiss();
                break;
            case R.id.lessTv:
                if (number > 1) {
                    number--;
                }
                numberTv.setText(number + "");
                mOnClickListener.updateNumber(number);
                break;
            case R.id.addTv:
                number++;
                numberTv.setText(number + "");
                mOnClickListener.updateNumber(number);
                break;


        }

    }

    public interface OnClickListener {
        void onSubmit(int ggId, String name, int type, int position);

        void updateNumber(int number);

    }

}
